<template>
  <div class="register-container">
    <form @submit.prevent="handleSubmit">
      <!-- 用户名 -->
      <div class="form-group">
        <label>用户名<span class="required">*</span></label>
        <input type="text" v-model.trim="formData.username"
               placeholder="请输入用户名(4-50字符)" required>
      </div>

      <!-- 密码 -->
      <div class="form-group">
        <label>密码<span class="required">*</span></label>
        <input type="password" v-model.trim="formData.password"
               placeholder="请输入密码(8-100字符)" required>
      </div>

      <!-- 手机号 -->
      <div class="form-group">
        <label>手机号<span class="required">*</span></label>
        <input type="tel" v-model.trim="formData.phone"
               placeholder="请输入手机号" pattern="^1[3-9]\d{9}$">
      </div>

      <!-- 邮箱 -->
      <div class="form-group">
        <label>邮箱</label>
        <input type="email" v-model.trim="formData.email"
               placeholder="请输入邮箱">
      </div>

      <!-- 用户类型 (隐藏字段) -->
      <input type="hidden" v-model="formData.user_type" value="1">

      <!-- 真实姓名 -->
      <div class="form-group">
        <label>真实姓名</label>
        <input type="text" v-model.trim="formData.real_name"
               placeholder="请输入真实姓名(2-30字符)">
      </div>

      <!-- 身份证号 -->
      <div class="form-group">
        <label>身份证号</label>
        <input type="text" v-model.trim="formData.id_card"
               placeholder="请输入身份证号(18位)" maxlength="18">
      </div>

      <!-- 头像上传 -->
      <div class="form-group">
        <label>头像</label>
        <div class="upload-area">
          <img v-if="formData.avatar" :src="formData.avatar" class="avatar-preview">
          <input type="file" accept="image/*" @change="handleAvatarUpload">
        </div>
      </div>

      <div class="form-footer">
        <button type="submit" :disabled="isSubmitting" class="submit-btn">
          {{ isSubmitting ? '提交中...' : '注册账号' }}
        </button>
      </div>
    </form>
  </div>
</template>

<script setup>
import { ref, reactive ,toRaw} from 'vue'

// 表单数据结构 (完整匹配数据库字段)
const formData = reactive({
  username: '',
  password: '',
  phone: '',
  email: '',
  user_type: 1,  // 默认普通用户(按图枚举值)
  real_name: '',
  id_card: '',
  avatar: ''    // 存储头像URL
})

// 表单提交状态
const isSubmitting = ref(false)

// 头像上传处理
const handleAvatarUpload = (e) => {
  const file = e.target.files[0]
  if (!file) return

  // 创建预览URL
  formData.avatar = URL.createObjectURL(file)

  // 实际项目需上传到服务器
  const uploadForm = new FormData()
  uploadForm.append('avatar', file)

}

// 提交注册表单
const handleSubmit = async () => {
  try {
    isSubmitting.value = true

    // 基本表单验证
    if (!validateForm()) return
    // 模拟注册逻辑 (实际项目需替换为API调用)
    const res = await axios.post('/api/register', toRaw(formData))

    console.log('注册成功', res.data)
    alert('注册成功！请登录')
    resetForm()

  } catch (error) {
    console.error('注册失败', error)
    alert(`注册失败: ${error.response?.data?.message || error.message}`)
  } finally {
    isSubmitting.value = false
  }
}

// 表单验证规则
const validateForm = () => {
  // 用户名验证
  if (formData.username.length < 4 || formData.username.length > 50) {
    alert('用户名长度需在4-50字符之间')
    return false
  }

  // 密码强度验证
  if (formData.password.length < 8 || formData.password.length > 100) {
    alert('密码长度需在8-100字符之间')
    return false
  }

  // 手机号格式验证
  if (formData.phone && !/^1[3-9]\d{9}$/.test(formData.phone)) {
    alert('手机号格式不正确')
    return false
  }

  return true
}

// 重置表单
const resetForm = () => {
  Object.assign(formData, {
    username: '',
    password: '',
    phone: '',
    email: '',
    real_name: '',
    id_card: '',
    avatar: ''
  })
}
</script>

<style scoped>
.register-container {
  max-width: 600px;
  margin: 0 auto;
  padding: 20px;
  background: #f8f9fa;
  border-radius: 8px;
}

.form-group {
  margin-bottom: 20px;
}

label {
  display: block;
  margin-bottom: 8px;
  font-weight: bold;
}

input[type="text"],
input[type="password"],
input[type="tel"],
input[type="email"] {
  width: 100%;
  padding: 10px;
  border: 1px solid #ced4da;
  border-radius: 4px;
  box-sizing: border-box;
}

.required {
  color: #ff4d4f;
  margin-left: 4px;
}

.upload-area {
  border: 1px dashed #d9d9d9;
  padding: 15px;
  text-align: center;
  cursor: pointer;
  border-radius: 4px;
  background: #fafafa;
}

.avatar-preview {
  max-width: 100px;
  max-height: 100px;
  margin-bottom: 10px;
}

.submit-btn {
  background: #1890ff;
  color: white;
  border: none;
  padding: 10px 20px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 16px;
}

.submit-btn:disabled {
  background: #a0d1ff;
  cursor: not-allowed;
}
</style>